package com.kucai.yifaqapp.presenter.activity;

import android.widget.Toast;

import com.kucai.yifaqapp.global.HttpAdress;
import com.kucai.yifaqapp.model.CustomObserver;
import com.kucai.yifaqapp.model.network.HttpJsonUtils;
import com.kucai.yifaqapp.model.network.entity.res.ResCode;
import com.kucai.yifaqapp.model.network.entity.res.ResLogin;
import com.kucai.yifaqapp.model.network.entity.res.Resregister;
import com.kucai.yifaqapp.ui.activity.RegisterActivity;
import com.kucai.yifaqapp.util.SYSOUtils;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class RegisterActivityPresenter extends BaseActivityPresenter<RegisterActivity> {
    public RegisterActivityPresenter(RegisterActivity mActivity) {
        super(mActivity);
    }

    /**
     * 验证码
     */
    public void getCode() {
        HttpJsonUtils.getHttpApiInstance()
                .getcode(HttpAdress.getuseAdress() + "api/m/captcha")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<ResCode>(mActivity) {
                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {

                    }

                    @Override
                    protected void onCustomNext(ResCode resCode) {
                        if (null != resCode.data)
                            mActivity.onCode(resCode);
                    }
                });
    }

    /**
     * 注册
     * (peuser, password, realname, phone, username, codes, code);
     */
    public void register(String puser, String password, final String realname, String phone, String username, String codes, String code) {
        HttpJsonUtils.getHttpApiInstance()
                .register(HttpAdress.getuseAdress() + "api/m/reg",puser, password, realname, phone, username, codes, code)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<Resregister>(mActivity) {
                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {
                        mActivity.onRegister();
                    }

                    @Override
                    protected void onCustomNext(Resregister resregister) {
                        if (resregister.code != 0) {
                            mActivity.onRegister();
                            Toast.makeText(mActivity, resregister.msg, Toast.LENGTH_SHORT).show();
                        } else {
                            mActivity.onUsermessage(resregister);
                        }
                    }
                });
    }

    /**
     * 轮训用户信息
     * (peuser, password, realname, phone, username, codes, code);
     */
    public void finduser(String token) {
        HttpJsonUtils.getHttpApiInstance()
                .finduser(HttpAdress.getuseAdress() + "api/m/user/getUserMsg",token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<ResLogin>(mActivity) {
                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {
                        Toast.makeText(mActivity, "查询失败", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    protected void onCustomNext(ResLogin resLogin) {
                        SYSOUtils.sOut_(resLogin.data);

                    }

                    @Override
                    public void onNext(ResLogin resLogin) {
                        super.onNext(resLogin);
                        Toast.makeText(mActivity, "注册成功", Toast.LENGTH_SHORT).show();
                        mActivity.finish();
                    }
                });
    }
}
